package com.macro.mall.portal.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.CmsContentMapper;
import com.macro.mall.model.CmsContent;
import com.macro.mall.portal.service.CmsContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * CmsContent的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class CmsContentServiceImpl implements CmsContentService {

    private boolean isDebugEnabled;

    @Autowired
    private CmsContentMapper cmsContentMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<CmsContent> find(CmsContent value) {
        //TODO这里可以做通过Assist做添加查询
        List<CmsContent> result = null;
        try {
            result = cmsContentMapper.selectCmsContent(null);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CmsContent数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public List<CmsContent> find(String title, String source, String channel,  String classified,  String popId, String status, String orderBy, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        assist.customRequire(" AND stime <= str_to_date(", format, ",'%Y-%m-%d %H:%i:%s')");
        assist.customRequire(" AND etime >= str_to_date(", format, ",'%Y-%m-%d %H:%i:%s')");

        if (!StrUtil.isEmpty(title)) {
            assist.andLike("title ", "%" + title + "%");
        }
        if (!StrUtil.isEmpty(source)) {
            assist.andEq("source", source);
        }
        if (!StrUtil.isEmpty(channel)) {
            assist.andLike("channel", "%" + channel +"%");
        }
        if (!StrUtil.isEmpty(classified)) {
            assist.andIn("classified", classified);
        }
        if(!StrUtil.isEmpty(popId)) {
            assist.andLike("pop_id","%" + popId +"%");
        }
        if (!StrUtil.isEmpty(status)) {
            assist.andEq("status", "4");
        }
        assist.andNeq("classified", "08");
        if (StrUtil.isNotEmpty(orderBy)) {
            PageHelper.orderBy(orderBy);
        }
        List<CmsContent> result = null;
        try {
            result = cmsContentMapper.selectCmsContent(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CmsContent数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public CmsContent findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CmsContent的id获得CmsContent对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过CmsContent的id获得CmsContent对象-->失败:id不能为空");
        }
        CmsContent result = null;
        try {
            result = cmsContentMapper.selectCmsContentById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CmsContent的id获得CmsContent对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(CmsContent value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将CmsContent中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            //弹屏没有来源
            if (StrUtil.isEmpty(value.getSource()) && !"10".equals(value.getClassified())) {
                Asserts.fail("来源不可为空");
            }
            //弹屏位置不可为空
            if ("10".equals(value.getClassified())&&StrUtil.isEmpty(value.getPopId())) {
                Asserts.fail("弹屏位置不可为空");
            }
            result = cmsContentMapper.insertNotNullCmsContent(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将CmsContent中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(CmsContent value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CmsContent的id更新CmsContent中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过CmsContent的id更新CmsContent中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = cmsContentMapper.updateNotNullCmsContentById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CmsContent的id更新CmsContent中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CmsContent的id删除CmsContent-->失败:id不能为空");
            }
            Asserts.fail("执行通过CmsContent的id删除CmsContent-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = cmsContentMapper.deleteCmsContentById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CmsContent的id删除CmsContent-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public void updateStatus(Integer id) {
        CmsContent cmsContent = new CmsContent();
        cmsContent.setId(id);
        cmsContent.setStatus("5");
        updateNotNullById(cmsContent);
    }


}